package com.sixth.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.sixth.annotation.PageInfoAdvice;
import com.sixth.bean.*;
import com.sixth.bean.bo.*;
import com.sixth.bean.common.BasePageInfoVo;
import com.sixth.bean.common.BaseRespVo;
import com.sixth.bean.vo.AddCouponVO;
import com.sixth.bean.vo.BaseListVO;
import com.sixth.bean.vo.CouponVO;
import com.sixth.bean.vo.UserCouponVO;
import com.sixth.constant.Constant;
import com.sixth.mapper.MarketCartMapper;
import com.sixth.mapper.MarketCouponMapper;
import com.sixth.mapper.MarketCouponUserMapper;
import com.sixth.service.CouponService;
import com.sixth.util.CouponUtil;
import com.sixth.util.MarketInfoUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author Chen
 * @Date 2022-09-07  19-57-28
 * @description 优惠券管理类
 */
@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    MarketCouponMapper couponMapper;

    @Autowired
    MarketCouponUserMapper couponUserMapper;

    @Autowired
    MarketCartMapper cartMapper;

    /**
     * @param
     * @return {@link null }
     * @Author Chen
     * @Date 2022-09-07  19-56-22
     * @description 根据条件查询优惠券
     */
    @Override
    public BaseListVO<MarketCoupon> getCouponList(CouponPageInfo couponPageInfo) {

        //更新过期优惠券状态
        MarketCoupon updateCouponStatus = new MarketCoupon();
        updateCouponStatus.setStatus(Constant.EXPIRED);
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteriaStatus = marketCouponExample.createCriteria();
        criteriaStatus.andEndTimeLessThan(new Date());
        couponMapper.updateByExampleSelective(updateCouponStatus,marketCouponExample);


        //开启分页
        PageHelper.startPage(couponPageInfo.getPage(), couponPageInfo.getLimit());

        MarketCouponExample couponExample = new MarketCouponExample();

        couponExample.setDistinct(true);

        if (StringUtil.isNotEmpty(couponPageInfo.getOrder())&&StringUtil.isNotEmpty(couponPageInfo.getSort())){
            couponExample.setOrderByClause(couponPageInfo.getSort() + " " + couponPageInfo.getOrder());
        }

        MarketCouponExample.Criteria criteria = couponExample.createCriteria();

        criteria.andDeletedEqualTo(false);
        //未过期的优惠券
        //criteria.andEndTimeGreaterThan(new Date());

        criteria.andEndTimeGreaterThanOrEqualTo(new Date());


        if (!StringUtil.isEmpty(couponPageInfo.getName())) {
            criteria.andNameLike("%" + couponPageInfo.getName() + "%");
        }

        if (couponPageInfo.getStatus() != null) {
            criteria.andStatusEqualTo((short) couponPageInfo.getStatus().intValue());
        }

        if (couponPageInfo.getType() != null) {
            criteria.andTypeEqualTo((short) couponPageInfo.getType().intValue());
        }

        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(couponExample);

        PageInfo pageInfo = new PageInfo(marketCoupons);

        BaseListVO<MarketCoupon> couponBaseListVO = new BaseListVO<>();

        couponBaseListVO.setList(marketCoupons);
        couponBaseListVO.setPage(pageInfo.getPageNum());
        couponBaseListVO.setPages(pageInfo.getPages());
        couponBaseListVO.setLimit(pageInfo.getPageSize());
        couponBaseListVO.setTotal(pageInfo.getTotal());


        return couponBaseListVO;
    }


    /**
     * @Author Chen
     * @Date 2022-09-12  22-34-35
     * @param
     * @return {@link null }
     * @description
     */
    @Override
    public BaseListVO<MarketCoupon> getWXCouponList(CouponPageInfo couponPageInfo) {

        //更新过期优惠券状态
        MarketCoupon updateCouponStatus = new MarketCoupon();
        updateCouponStatus.setStatus(Constant.EXPIRED);
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteriaStatus = marketCouponExample.createCriteria();
        criteriaStatus.andEndTimeLessThan(new Date());
        couponMapper.updateByExampleSelective(updateCouponStatus,marketCouponExample);


        //开启分页
        PageHelper.startPage(couponPageInfo.getPage(), couponPageInfo.getLimit());

        MarketCouponExample couponExample = new MarketCouponExample();

        couponExample.setDistinct(true);

        if (StringUtil.isNotEmpty(couponPageInfo.getOrder())&&StringUtil.isNotEmpty(couponPageInfo.getSort())){
            couponExample.setOrderByClause(couponPageInfo.getSort() + " " + couponPageInfo.getOrder());
        }

        MarketCouponExample.Criteria criteria = couponExample.createCriteria();

        criteria.andDeletedEqualTo(false);

        criteria.andTypeEqualTo(Constant.ZERO);
        //未过期的优惠券
        criteria.andEndTimeGreaterThan(new Date());

        if (!StringUtil.isEmpty(couponPageInfo.getName())) {
            criteria.andNameLike("%" + couponPageInfo.getName() + "%");
        }

        if (couponPageInfo.getStatus() != null) {
            criteria.andStatusEqualTo((short) couponPageInfo.getStatus().intValue());
        }

        if (couponPageInfo.getType() != null) {
            criteria.andTypeEqualTo((short) couponPageInfo.getType().intValue());
        }

        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(couponExample);

        PageInfo pageInfo = new PageInfo(marketCoupons);

        BaseListVO<MarketCoupon> couponBaseListVO = new BaseListVO<>();

        couponBaseListVO.setList(marketCoupons);
        couponBaseListVO.setPage(pageInfo.getPageNum());
        couponBaseListVO.setPages(pageInfo.getPages());
        couponBaseListVO.setLimit(pageInfo.getPageSize());
        couponBaseListVO.setTotal(pageInfo.getTotal());


        return couponBaseListVO;
    }

    /**
     * @param
     * @return {@link null }
     * @Author Chen
     * @Date 2022-09-07  19-57-05
     * @description 更新优惠券
     */
    @Override
    public MarketCoupon updateCoupon(CouponBO couponBO) {

        MarketCoupon coupon = new MarketCoupon();
        coupon.setId(couponBO.getId());
        coupon.setAddTime(couponBO.getAddTime());
        coupon.setCode(coupon.getCode());
        coupon.setDays(couponBO.getDays());
        coupon.setDeleted(couponBO.getDeleted());
        coupon.setDesc(couponBO.getDesc());
        coupon.setDiscount(couponBO.getDiscount());
        coupon.setEndTime(couponBO.getEndTime());
        coupon.setGoodsType(couponBO.getGoodsType());
        coupon.setGoodsValue(couponBO.getGoodsValue());
        coupon.setLimit(couponBO.getLimit());
        coupon.setMin(couponBO.getMin());
        coupon.setName(couponBO.getName());
        coupon.setStartTime(couponBO.getStartTime());
        coupon.setStatus(couponBO.getStatus());
        coupon.setTag(couponBO.getTag());
        coupon.setTimeType(couponBO.getTimeType());
        coupon.setTotal(couponBO.getTotal());
        coupon.setType(couponBO.getType());
        coupon.setUpdateTime(couponBO.getUpdateTime());

        int updateResult = couponMapper.updateByPrimaryKey(coupon);

        if (updateResult == Constant.ONE) {

            return coupon;
        }

        return null;
    }

    /**
     * @param
     * @return {@link null }
     * @Author Chen
     * @Date 2022-09-07  20-20-29
     * @description 根据ID查询优惠券详情
     */
    @Override
    public CouponVO getCouponDetail(Integer id) {

        MarketCoupon coupon = couponMapper.selectByPrimaryKey(id);

        if (coupon == null) {
            return null;
        }

        CouponVO couponVO = new CouponVO();
        couponVO.setAddTime(coupon.getAddTime());
        couponVO.setDays(coupon.getDays());
        couponVO.setDeleted(coupon.getDeleted());
        couponVO.setDesc(coupon.getDesc());
        couponVO.setDiscount(coupon.getDiscount());
        couponVO.setGoodsType(coupon.getGoodsType());
        couponVO.setGoodsValue(coupon.getGoodsValue());
        couponVO.setId(coupon.getId());
        couponVO.setLimit(coupon.getLimit());
        couponVO.setMin(coupon.getMin());
        couponVO.setName(coupon.getName());
        couponVO.setStatus(coupon.getStatus());
        couponVO.setTag(coupon.getTag());
        couponVO.setTimeType(coupon.getTimeType());
        couponVO.setTotal(coupon.getTotal());
        couponVO.setType(coupon.getType());
        //兑换码优惠券
        couponVO.setCode(coupon.getCode());

        couponVO.setUpdateTime(coupon.getUpdateTime());


        return couponVO;
    }

    /**
     * @param
     * @return {@link null }
     * @Author Chen
     * @Date 2022-09-07  21-37-02
     * @description 查询那些用户领取了此优惠券
     */
    @Override
    public BaseListVO<MarketCouponUser> getCouponListUser(CouponUserPage couponUserPage) {


        //TIP:开启分页
        PageHelper.startPage(couponUserPage.getPage(),couponUserPage.getLimit());

        MarketCouponUserExample couponUserExample = new MarketCouponUserExample();

        couponUserExample.setDistinct(true);

        couponUserExample.setOrderByClause(couponUserPage.getSort() + " " + couponUserPage.getOrder());

        MarketCouponUserExample.Criteria criteria = couponUserExample.createCriteria();

        if (couponUserPage.getCouponId() != null && !StringUtil.isEmpty(couponUserPage.getCouponId().toString())) {
            criteria.andCouponIdEqualTo(couponUserPage.getCouponId());
        }

        if (couponUserPage.getUserId() != null && !StringUtil.isEmpty(couponUserPage.getUserId().toString())) {
            criteria.andUserIdEqualTo(couponUserPage.getUserId());
        }

        if (couponUserPage.getStatus() != null && !StringUtil.isEmpty(couponUserPage.getStatus().toString())) {
            criteria.andStatusEqualTo(couponUserPage.getStatus());
        }

        List<MarketCouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);

        PageInfo pageInfo = new PageInfo(couponUsers);
        BaseListVO<MarketCouponUser> couponUserListVO = new BaseListVO<>();

        couponUserListVO.setPage(pageInfo.getPageNum());
        couponUserListVO.setPages(pageInfo.getPages());
        couponUserListVO.setLimit(pageInfo.getPageSize());
        couponUserListVO.setTotal(pageInfo.getTotal());
        couponUserListVO.setList(couponUsers);

        return couponUserListVO;
    }

    /**
     * @Author Chen
     * @Date 2022-09-07  22-11-15
     * @param
     * @return {@link null }
     * @description 逻辑删除优惠券
     */
    @Override
    public int delCoupon(CouponDelBO couponDelBO) {

        MarketCoupon coupon = new MarketCoupon();

        BeanUtils.copyProperties(couponDelBO,coupon);

        coupon.setDeleted(true);

        int updateResult = couponMapper.updateByPrimaryKey(coupon);

        MarketCouponUser marketCouponUser = new MarketCouponUser();
        marketCouponUser.setStatus(Constant.COUPON_STATUS_REMOVED);
        MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = marketCouponUserExample.createCriteria();
        criteria.andCouponIdEqualTo(couponDelBO.getId());

        couponUserMapper.updateByExampleSelective(marketCouponUser,marketCouponUserExample);

        if (updateResult == Constant.ONE){
            return Constant.OK;
        }

        return Constant.NON;
    }

    /**
     * @Author Chen
     * @Date 2022-09-07  23-24-28
     * @param
     * @return {@link null }
     * @description 添加优惠券
     */
    @Override
    public AddCouponVO addCoupon(AddCouponBO addCouponBO) {


        MarketCoupon coupon = new MarketCoupon();

        BeanUtils.copyProperties(addCouponBO,coupon);

        String goodsValue = "[]";
        Integer[] valueArr = addCouponBO.getGoodsValue();
        if (valueArr.length>0){
            goodsValue="[";
            for (Integer integer : valueArr) {
                goodsValue = goodsValue+""+integer+",";
            }
            goodsValue = goodsValue.substring(0,goodsValue.length()-1);
            goodsValue = goodsValue+"]";
        }

        //添加优惠券时日期
        Date nowDate = new Date();
        coupon.setGoodsValue(goodsValue);
        coupon.setAddTime(nowDate);
        coupon.setUpdateTime(nowDate);
        if (addCouponBO.getType()==Constant.COUPON_TYPE_CODE){
            String couponCode = CouponUtil.getRandomCode();
            coupon.setCode(couponCode);
        }

        if (addCouponBO.getTimeType()==Constant.ONE){
            coupon.setStartTime(addCouponBO.getStartTime());
            coupon.setEndTime(addCouponBO.getEndTime());
        }else {
            coupon.setStartTime(nowDate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(nowDate);
            calendar.add(Calendar.DAY_OF_YEAR,addCouponBO.getDays());
            Date endTime = calendar.getTime();
            coupon.setEndTime(endTime);
        }
        coupon.setDeleted(false);

        int insertResult = couponMapper.insert(coupon);


        if (insertResult == Constant.ONE){
            addCouponBO.setId(coupon.getId());
            AddCouponVO addCouponVO = new AddCouponVO();
            BeanUtils.copyProperties(addCouponBO,addCouponVO);
            return addCouponVO;
        }

        return null;
    }

    /**
     * @Author Chen
     * @Date 2022-09-10  14-15-20
     * @param
     * @return {@link null }
     * @description 小程序端领取优惠券
     */
    @Override
    public int receiveCoupon(Integer couponId,Integer userId) {

        MarketCoupon coupon = couponMapper.selectByPrimaryKey(couponId);

        if (coupon!=null){

            MarketCouponUserExample couponUserExample = new MarketCouponUserExample();

            MarketCouponUserExample.Criteria criteria = couponUserExample.createCriteria();

            criteria.andStatusEqualTo(Constant.ZERO);

            criteria.andDeletedEqualTo(false);

            criteria.andUserIdEqualTo(userId);

            criteria.andCouponIdEqualTo(couponId);

            List<MarketCouponUser> marketCouponUsers = couponUserMapper.selectByExample(couponUserExample);

            //限制用户只能领取优惠券数量
            if (marketCouponUsers.size()>=coupon.getLimit()){
                return Constant.RECEIVED;
            }

            MarketCouponUser marketCouponUser = new MarketCouponUser();
            marketCouponUser.setUserId(userId);
            marketCouponUser.setCouponId(couponId);
            marketCouponUser.setStatus(Constant.UNUSEED);
            // 商家优惠券活动开始时间
            marketCouponUser.setStartTime(coupon.getStartTime());

            //用户领取时间
            Date receiveDate = new Date();
            marketCouponUser.setAddTime(receiveDate);
            if (coupon.getTimeType()==Constant.ZERO){
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(receiveDate);
                calendar.add(Calendar.DAY_OF_YEAR,coupon.getDays());
                marketCouponUser.setEndTime(calendar.getTime());
            }else {
                marketCouponUser.setEndTime(coupon.getEndTime());
            }

            marketCouponUser.setDeleted(false);
            marketCouponUser.setUpdateTime(new Date());

            couponUserMapper.insertSelective(marketCouponUser);

            return Constant.OK;
        }



        return Constant.NON;
    }



    /**
     * @Author Chen
     * @Date 2022-09-12  10-57-29
     * @param
     * @return {@link null }
     * @description 用户查看自己的优惠券
     */
    @PageInfoAdvice
    @Override
    public Object getMyCoupons(CouponPageInfo couponPageInfo,Integer userId) {

        //更新用户优惠券的状态
        MarketCouponUser marketCouponUser = new MarketCouponUser();
        marketCouponUser.setStatus(Constant.COUPON_TYPE_CODE);
        MarketCouponUserExample example = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteriaU = example.createCriteria();
        criteriaU.andEndTimeLessThan(new Date());
        couponUserMapper.updateByExampleSelective(marketCouponUser,example);

        //用户查询优惠券
        MarketCouponUserExample couponUserExample = new MarketCouponUserExample();
        couponUserExample.setDistinct(true);
        MarketCouponUserExample.Criteria criteria = couponUserExample.createCriteria();
        criteria.andStatusEqualTo(couponPageInfo.getStatus().shortValue());
        criteria.andDeletedEqualTo(false);
        criteria.andUserIdEqualTo(userId);
        //用户查询未过期的优惠券
        criteria.andEndTimeGreaterThan(new Date());

        List<MarketCouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);

        //当用户没有优惠券时，返回给Controller:null，让Controller层返回给客户端提示
        if (!(couponUsers.size()>=Constant.ONE)){
            return couponUsers;
        }

        ArrayList<Integer> couponIds = new ArrayList<>();
        for (MarketCouponUser couponUser : couponUsers) {

            couponIds.add(couponUser.getCouponId());
        }

        //用户领取多张同一优惠券时，只要没有超过该优惠券的用户领取限制，便可以获得多张优惠券
        List<MarketCoupon> marketCoupons = new ArrayList<>();
        for (Integer couponId : couponIds) {
            MarketCouponExample marketCouponExample = new MarketCouponExample();
            MarketCouponExample.Criteria userCouponCriteria = marketCouponExample.createCriteria();
            userCouponCriteria.andDeletedEqualTo(false);

            userCouponCriteria.andIdEqualTo(couponId);

            //用户查询未过期的优惠券
            //userCouponCriteria.andEndTimeGreaterThan(new Date());

            List<MarketCoupon> coupons = couponMapper.selectByExample(marketCouponExample);

            marketCoupons.addAll(coupons);
        }

        ArrayList<UserCouponVO> userCouponVOS = new ArrayList<>();
        if (marketCoupons.size()>=Constant.ONE){

            for (MarketCoupon marketCoupon : marketCoupons) {
                UserCouponVO userCouponVO = new UserCouponVO();
                BeanUtils.copyProperties(marketCoupon,userCouponVO);
                userCouponVO.setCid(marketCoupon.getId());
                userCouponVO.setAvailable(false);
                userCouponVOS.add(userCouponVO);
            }


        }

        return userCouponVOS;
    }


    /**
     * @Author Chen
     * @Date 2022-09-12  10-57-52
     * @param
     * @return {@link null }
     * @description 用户通过兑换码兑换优惠券
     */
    @Override
    public int exchangeCoupon(CouponCodeBO couponCodeBO) {

        MarketUser marketUser = MarketInfoUtil.getMarketUser();
        if (marketUser == null||marketUser.getId()==null){
            return Constant.NON;
        }

        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andTypeEqualTo(Constant.COUPON_TYPE_CODE);
        criteria.andStatusEqualTo(Constant.COUPON_STATUS_USABLE);
        criteria.andCodeEqualTo(couponCodeBO.getCode());

        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(marketCouponExample);
        if (marketCoupons.size()==Constant.ONE){

            MarketCoupon coupon = marketCoupons.get(0);
            MarketCouponUser marketCouponUser = new MarketCouponUser();
            marketCouponUser.setUserId(marketUser.getId());
            marketCouponUser.setCouponId(coupon.getId());
            marketCouponUser.setStatus(Constant.UNUSEED);
            marketCouponUser.setAddTime(new Date());
            marketCouponUser.setStartTime(coupon.getStartTime());
            marketCouponUser.setEndTime(coupon.getEndTime());
            marketCouponUser.setDeleted(false);
            marketCouponUser.setUpdateTime(new Date());

            couponUserMapper.insertSelective(marketCouponUser);

            return Constant.OK;
        }

        return Constant.NON;
    }


    /**
     * @Author Chen
     * @Date 2022-09-12  16-42-59
     * @param
     * @return {@link null }
     * @description  用户下单时，选择使用哪一张优惠券，只显示当前商品可用的优惠券
     */

    //TODO:等待测试

    @Override
    public BaseListVO<UserCouponVO> selectList(Integer cartId) {

        MarketUser marketUser = MarketInfoUtil.getMarketUser();
        if (marketUser==null||marketUser.getId()==null){
            return null;
        }

        BigDecimal orderMoney = new BigDecimal(Constant.ZERO);
        if (cartId!=Constant.ZERO) {
            //从商品页面选择优惠券，会使用具体cartId
            MarketCart marketCart = cartMapper.selectByPrimaryKey(cartId);
            Short number = marketCart.getNumber();
            BigDecimal goodsNum = new BigDecimal(number);
            BigDecimal goodsPrice = marketCart.getPrice();

            orderMoney = goodsNum.multiply(goodsPrice);
        }else{
            //从购物车中选择优惠券，cartId此时为零
            MarketCartExample marketCartExample = new MarketCartExample();
            MarketCartExample.Criteria criteria = marketCartExample.createCriteria();
            MarketUser user = MarketInfoUtil.getMarketUser();
            if (user==null){
                return null;
            }
            criteria.andUserIdEqualTo(user.getId());
            criteria.andCheckedEqualTo(true);
            criteria.andDeletedEqualTo(false);
            List<MarketCart> marketCarts = cartMapper.selectByExample(marketCartExample);
            for (MarketCart marketCart : marketCarts) {
                BigDecimal goodsNum = new BigDecimal(marketCart.getNumber());
                BigDecimal goodsPrice = marketCart.getPrice();
                //todo 这行代码不能add？
                orderMoney = orderMoney.add(goodsNum.multiply(goodsPrice));
            }
        }

            MarketCouponUserExample couponUserExample = new MarketCouponUserExample();
            MarketCouponUserExample.Criteria criteria = couponUserExample.createCriteria();
            criteria.andStatusEqualTo(Constant.UNUSEED);
            criteria.andDeletedEqualTo(false);
            criteria.andUserIdEqualTo(marketUser.getId());
            criteria.andEndTimeGreaterThan(new Date());

            List<MarketCouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);
            if (!(couponUsers.size()>=Constant.ONE)){
                return null;
            }

            ArrayList<Integer> couponIds = new ArrayList<>();
            for (MarketCouponUser couponUser : couponUsers) {

                couponIds.add(couponUser.getCouponId());
            }


            //用户领取多张同一优惠券时，只要没有超过该优惠券的用户领取限制，便可以获得多张优惠券
            List<MarketCoupon> marketCoupons = new ArrayList<>();
            for (Integer couponId : couponIds) {

                MarketCouponExample marketCouponExample = new MarketCouponExample();

                MarketCouponExample.Criteria userCouponCriteria = marketCouponExample.createCriteria();

                userCouponCriteria.andDeletedEqualTo(false);

                userCouponCriteria.andIdEqualTo(couponId);

                userCouponCriteria.andEndTimeGreaterThan(new Date());

                //用户选择可用优惠券时，只能使用满足最低消费金额的优惠券
                userCouponCriteria.andMinLessThan(orderMoney);

                List<MarketCoupon> coupons = couponMapper.selectByExample(marketCouponExample);


                for (MarketCoupon coupon : coupons) {
                    if (orderMoney.compareTo(coupon.getMin())==Constant.ONE||orderMoney.compareTo(coupon.getMin())==Constant.ZERO){
                        marketCoupons.add(coupon);
                    }
                }

            }



            if (marketCoupons.size()>=Constant.ONE){
                ArrayList<UserCouponVO> userCouponVOS = new ArrayList<>();
                for (MarketCoupon marketCoupon : marketCoupons) {
                    UserCouponVO userCouponVO = new UserCouponVO();
                    BeanUtils.copyProperties(marketCoupon,userCouponVO);
                    userCouponVO.setCid(marketCoupon.getId());
                    userCouponVO.setAvailable(true);
                    userCouponVOS.add(userCouponVO);
                }

                PageInfo<UserCouponVO> pageInfo = new PageInfo<>(userCouponVOS);

                BaseListVO<UserCouponVO> baseListVO = new BaseListVO<>();
                baseListVO.setList(userCouponVOS);
                baseListVO.setLimit(pageInfo.getSize());
                baseListVO.setPages(pageInfo.getPages());
                baseListVO.setPage(pageInfo.getPageNum());
                baseListVO.setTotal(pageInfo.getTotal());
                return baseListVO;
            }


        return null;
    }

}
